<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Users;
use App\Models\Linkslists;
use App\Models\Memberorders;
use App\Models\Usersinvitations;
use App\Services\WechatService;
use MarkdownEditor,Purifier,Cookie,Log,Pay;

class OrdersController extends Controller
{
    public function userOrders(Request $request)
    {
        $orders = Memberorders::getUsersOrders($request->users_id);
        return response()->json(['status' => 'success', 'msg' => '已加载订单列表', 'orders' => $orders]);
    }

    public function agentOrders(Request $request)
    {
        // 邀请记录的订单
        $orders = Usersinvitations::getagentOrders($request->users_id);
        return response()->json(['status' => 'success', 'msg' => '已加载订单列表', 'orders' => $orders]);
    }

    public function agentPrice(Request $request)
    {
        // 获取订单
        $orders = Memberorders::getByOrderNo($request->order_no);
        $users = Users::getUsersDetial($id);
        if ($users->authority < 2) return response()->json(['status' => 'error','code'=>1001,'msg' => '您无权执行本操作！']);
        $invitations = Usersinvitations::checkAgentUsers($request->users_id,$orders->users_id);
        if (empty($invitations)) return response()->json(['status' => 'error','code'=>1001,'msg' => '您无权执行本操作！']);
        $products = Memberorders::$agentproducts;
        $product = collect($products)->where("id",$orders->product_id)->first();
        if ($request->price < $product->price) return response()->json(['status' => 'error','code'=>1002,'msg' => '产品价格不得低于原价！']);
        if ($request->price > $product->price_original) return response()->json(['status' => 'error','code'=>1002,'msg' => '产品价格不得高于当前价格！']);
        // 更改产品价格
        Memberorders::updatePayStatus($request->order_no,["price"=>$request->price]);
        return response()->json(['status' => 'success', 'msg' => '订单金额修改成功！']);
    }

    public function ordersRepay(Request $request)
    {
        $orders = Memberorders::ordersDetialById($request->id);
        if (empty($orders)) return response()->json(["status"=>"error","code"=>"1001","msg"=>"订单不存在！"]);
        $user  = Users::getUsersById($request->users_id);
        $out_trade_no = md5(time().rand("10000","99999"));
        Memberorders::updateOrdersmemberInfo($request->id,["order_no"=>$out_trade_no]);
        if ($request->platefrom == "App") {
            $order  = [
                'out_trade_no' => $out_trade_no,
                'total_amount' => $orders->price,
                'subject'      => $orders->name,
                'passback_params'=>'member',
            ];
            $result = Pay::alipay(config('pay.alipay'))->app($order)->getContent();
        } else {
            $order = [
                'out_trade_no' => $out_trade_no,
                'total_fee' => $orders->price*100,
                'body' => $orders->name,
                'attach' => 'member',
                'openid' => $user->openid,
            ];
            try {
                $result = Pay::wechat(config('pay.wechat'))->miniapp($order);
            } catch (Exception $e) {
                return response()->json(["status"=>"error","code"=>"1001","msg"=>"支付失败,请稍后重试！"]);
            }
        }
        return response()->json(["status"=>"success","code"=>"1000","msg"=>"订单提交成功","data"=>["member_time"=>$orders->end_time,"wechat"=>$result]]);
    }
}
